package io.renren.modules.jxgkphone.controller;


import io.renren.modules.jxgk.entity.ClassDiaryTeacherSum;
import io.renren.modules.jxgk.service.TeacherEvaluationSumService;
import io.renren.modules.jxgk.utils.UUidCreater;
import io.renren.modules.jxgkphone.entity.EvaluationBody;
import io.renren.modules.jxgkphone.entity.EvaluationContent;
import io.renren.modules.jxgkphone.entity.EvaluationType;
import io.renren.modules.jxgkphone.entity.TeachingEvaluation;
import io.renren.modules.jxgkphone.service.TeachingEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/evaluation")
public class TeachingEvaluationController {
    @Autowired
    TeachingEvaluationService teachingEvaluationService;
    @Autowired
    TeacherEvaluationSumService teacherEvaluationSumService;

    @RequestMapping("/ContentInfo")
    public List SelectAllEvaluationContent() {
        List te = teachingEvaluationService.findAllEvaluationContent();
        return te;
    }
    @RequestMapping("{sid}/TeacherInfo")
    public List SelectTeacherInfo(@PathVariable("sid")String sid) {
        List te = teachingEvaluationService.findAllEvaluationContent();
        return te;
    }
    @RequestMapping("{tid}/{sid}/{cid}/EvaluationInfo")
    public List<EvaluationContent> SelectTeacherInfo(@PathVariable("tid")String tid,
                                  @PathVariable("sid")String sid,
                                  @PathVariable("cid")String cid) {
        List<TeachingEvaluation> te = teachingEvaluationService.findEvaluationInfo(tid,sid,cid);
        List<EvaluationContent> ec=new ArrayList<>();
        for(TeachingEvaluation tee:te){
            String ecid=tee.getEvaluationContent();
            EvaluationContent et=teachingEvaluationService.findEvaluationContentById(ecid);
            ec.add(et);
        }
        return ec;
    }





    @RequestMapping("/ContentInfo2")
    public Object SelectAllEvaluationContent2() {
        return teachingEvaluationService.findAllEvaluationContentMap();
    }

    @RequestMapping("{teacherId}/{courseId}/{studentSchoolId}/{evaluationTime}/ContentInfo3")
    public Object SelectAllEvaluationContent3(@PathVariable("courseId") String courseId, @PathVariable("teacherId") String teacherId, @PathVariable("studentSchoolId")String studentSchoolId, @PathVariable("evaluationTime") String evaluationTime, Map<String,Object> map) {
        //查询列表数据
        if(map==null) {
            map = new HashMap<String, Object>();
        }
        List<ClassDiaryTeacherSum> cse = null;
        if(courseId!=null&&!courseId.equals("")){
            map.put("courseId",courseId);
        }
        if(teacherId!=null&&!teacherId.equals("")){
            map.put("teacherId",teacherId);
        }
        if(studentSchoolId!=null&&!studentSchoolId.equals("")){
            map.put("studentSchoolId",studentSchoolId);
        }
        if(evaluationTime!=null&&!evaluationTime.equals("")){
            map.put("evaluationTime",evaluationTime);
        }
        Object object=teacherEvaluationSumService.findAllEvaluationContentMap(map);
        return object;
    }




    @RequestMapping("/insertEvaluation")
    public String InsertTeachingEvaluation(@RequestBody TeachingEvaluation te) {
        String teacher_id=te.getTeacherId();
        String student_id=te.getStudentId();
        String course_id=te.getCourseId();
        //te.setEvaluationTime(new Date().toString());
        List<TeachingEvaluation> teachingEvaluations=teachingEvaluationService.findEvaluationInfo(teacher_id,student_id,course_id);
        if(!teachingEvaluations.isEmpty()){
            teachingEvaluationService.deleteEvaluationInfo(teacher_id,student_id,course_id);
        }
        String content=te.getEvaluationContent();
        String [] contentArray=content.split(",");
        int i=0;
        for(;i<contentArray.length;i++){
            TeachingEvaluation temp=new TeachingEvaluation();
            UUidCreater uUidCreater=new UUidCreater();
            temp.setEvaluationId( uUidCreater.getUUID());
            temp.setTeacherId(te.getTeacherId());
            temp.setStudentId(te.getStudentId());
            temp.setCourseId(te.getCourseId());
            temp.setEvaluationContent(contentArray[i]);
            temp.setEvaluationTime(te.getEvaluationTime());
            teachingEvaluationService.InsertTeachingEvaluation(temp);
        }
        return i+"操作完成";

    }


    @RequestMapping("/TypeInfo")
    public List<EvaluationType> SelectAllEvaluationType() {
        List<EvaluationType> et = teachingEvaluationService.findAllEvaluationType();
        return et;
    }
    @RequestMapping("/Test")
    public  TeachingEvaluation Test() {
        TeachingEvaluation te= new TeachingEvaluation();
        te.setEvaluationId("1452323");
        te.setEvaluationTime("11点");
        te.setStudentId("1234");
        te.setTeacherId("4321");
        te.setEvaluationContent("201,301,401,221");
        te.setCourseId("12345");
        return te;
        //EvaluationBody evaluationBody=new EvaluationBody();
        //Map a=evaluationBody.GeneratData();
        //return a;
    }
}